package com.mzj.solr;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;

import com.mzj.util.PropertiesUtils;

public class SubscribeLogMove {

	private static final Logger LOG = Logger.getLogger(SolrMoveEs.class);  

	public static Map<String, String> paramMap = null;
	
	static {
		paramMap = PropertiesUtils.getParamMap();
	}
	
	
	private static SolrQuery setRequest(SolrQuery query, String index) {
		String start = paramMap.get("other.start.time");
		String end = paramMap.get("other.end.time");
		
		if(index.equals("subscribe_log")) {
			query.setFilterQueries("create_time:["+start+" TO "+end+"]");
		}else if(index.equals("weighthistory_day")){
			query.setFilterQueries("create_time:["+start+" TO "+end+"]");
		}else if(index.equals("weight_user")){
			query.setFilterQueries("req_time:["+start+" TO "+end+"]");
		}else {
			query.setFilterQueries("stat_date:["+start+" TO "+end+"]");
		}
		return query;
	}
	
	public static Long getTotal(SolrClient solrClient, String indexName) {
		SolrQuery query = new SolrQuery("*:*");  
		query = setRequest(query,indexName);
		query.setRows(1);
		query.setStart(0);
        QueryResponse resp;
		try {
			resp = solrClient.query(query);
			return resp.getResults().getNumFound();
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		}  
		return 0L;
	}

	public static void move(SolrClient solrClient, RestHighLevelClient esClient, String indexName, Integer pageSize,
			String indexName2, int start) throws Exception {
		SolrQuery query = new SolrQuery("*:*");  
		query = setRequest(query,indexName);
		query.setRows(pageSize);
		query.setStart(start);
		LOG.info("--------------query: start,"+start+" end,"+(start+pageSize));
		//query.addSort("create_time", ORDER.asc);
        QueryResponse resp = solrClient.query(query);  
        
        int size = resp.getResults().size();
        SolrDocumentList list = resp.getResults();
        BulkRequest request = new BulkRequest(); 
        for(int i=0; i<size; i++) {
        	SolrDocument doc = list.get(i);
        	Collection<String> ks = doc.getFieldNames();
        	Map<String,Object> input = new HashMap<String,Object>();
        	for (String k : ks) {
        		if(!"_version_".equals(k)){
        			input.put(k, doc.getFieldValue(k));
        		}
        	}  
        	request.add(new IndexRequest(indexName+"1", "data",input.get("id").toString()).source(input));
        }
        request.timeout(TimeValue.timeValueMinutes(20));
        BulkResponse bulk = esClient.bulk(request);
		
		if(bulk.hasFailures()){
			bulk = esClient.bulk(request);
			if(bulk.hasFailures()){
				LOG.info("====================================ES data has submited fail! "+start );
				LOG.info("====================================ES data has submited fail! "+bulk.buildFailureMessage());
			}
		}else {
			LOG.info("====================================ES data has submited sucess! "+start );
		}
		System.gc();
	}

}
